<?php

/**
 * @author: Kiennt
 * @name: dbsmart action database
 *
 */
class dbsmart{

	protected $db;
	/**
	 * Construction for class
	 */
	function __construct($dsn, $usr, $pwd){
		try{
			$this->db=new PDO($dsn, $usr, $pwd);
			$this->db->query("SET NAMES 'utf8'");
		}catch (PDOException $e){
			die("Not connected to host | ".$e->getMessage());
			
		}
	}

	/**
	 * @param  $sql SQL instruction
	 * @return PDOStatement
	 */
	function execSQL($sql){
		global $db;
		//$sql=addslashes($sql);
		$sth=$this->db->prepare($sql);
		$sth->execute();
		return $sth;
	}

	/**
	 * @param $sql
	 * @return two dementionmatrix
	 */
	function fetchAll($sql=null){
		
		global $db;
		$sth=$this->execSQL($sql);
		return $sth->fetchAll(PDO::FETCH_ASSOC);
	}
	
	
	/**
	 * @param $sql sql statament
	 * @return number row
	 */
	
	function fetchOne($sql){
		global $db;
		$sth=$this->execSQL($sql);
		return $sth->fetch(PDO::FETCH_ASSOC);
	}
	
	/**
	 * @param  $sql
	 * @return mixed
	 */
	function getDataToArray($sql,$getField){
		$arr = array();
		$sth=$this->execSQL($sql);
		while($row = $sth->fetch(PDO::FETCH_ASSOC)){
			$arr[] = $row[$getField];
		}
		return $arr;
	}
	
	

	/**
	 * @param $tbl name table
	 * @param $data is array('fiedl'=>value);
	 * @return PDOStatement
	 * @example insert('tbladmin', array('adm_usr'=>'kiennt','adm_pwd'=>'***'))
	 */
	function insert($tbl='',$data=array()){
		$listFields=array();
		$listValues=array();
		$sqlFields='';
		$sqlValues='';
		foreach($data as $k=>$v){
			$listFields[]=$k;
			$listValues[]="'".$v."'";
		}
		//print_r($listValues);
		$sqlFields=@implode(',',$listFields);
		$sqlValues=@implode(',',$listValues);
		$sql="INSERT INTO $tbl(".$sqlFields.") VALUES(".$sqlValues.");";
		
		$this->db->query($sql);
		$id = $this->db->lastInsertId();
		return $id;
	}
	
	/**
	 * @param  $tbl
	 * @param  $data
	 * @param  $where
	 * @return PDOStatement
	 * @example update('tblAdmin', array('adm_usr'=>'kiennt','adm_pwd'=>'***'), 'adm_id=101' )
	 */
	function update($tbl='',$data=array(), $where=null){
		global $db;
		$listFieldsValues=array();
		$sqlFieldsValues='';
		$where=($where!=null)?$where: ' 1=1 ';
		foreach($data as $k=>$v){
			$listFieldsValues[]=$k."='".$v."'";
		}
		$sqlFieldsValues=@implode(',',$listFieldsValues);
		$sql="UPDATE $tbl SET ".$sqlFieldsValues." WHERE ".$where;
		
		return $this->db->query($sql);
	}
	
	/**
	 * @param  $tbl
	 * @param  $where
	 * @return PDOStatement
	 */
	function delete($tbl, $where=null){
		$where=($where!=null)?$where: ' 1=1 ';
		$sql="DELETE FROM $tbl WHERE $where";
		return $this->db->query($sql);
	}
	function lastId(){
		$sql="SELECT bki_id FROM tblbooking WHERE 1=1";
		$rs=$this->fetchAll($sql);
		foreach($rs as $row){
			$id=$row['bki_id'];
		}
		/*if($id==""){
			$id="01";
		}*/
		$id=($id=="")?"01":$id;
		return $id;
	}
	
	function exitsRecord($tbl,$where){
		$e=0;
		$sql="SELECT * FROM ".$tbl." WHERE $where ";
		if($this->fetchOne($sql)){
			$e=1;
		}
		return $e;
	}

}
?>
